package com.hfb.mashibing.alip8.designpattern._019prototype;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 浅拷贝 V1
 * 深拷贝 V2
 */
public class MainV2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person p1 = new Person(23, 85, new Location("天津", 18));
        // Cloneable 是浅拷贝
        Person p2 = (Person) p1.clone();
        System.out.println(p2.toString());
        System.out.println(p2.loc);

        System.out.printf("p1 =?= p2 答：%s\n",p1 == p2);
        System.out.printf("p1.loc =?= p2.loc 答：%s\n",p1.loc == p2.loc);
        p1.loc.street = "sh";
        System.out.println(p2.toString());
    }
    @Data
    @AllArgsConstructor
    static
    class Person implements Cloneable {
        int age;
        int score;
        Location loc = new Location("北京", 22);
        @Override
        protected Object clone() throws CloneNotSupportedException {
            Person clonePerson = (Person) super.clone();
            clonePerson.loc = (Location) loc.clone();
            return clonePerson;
        }
    }

    @Data
    @AllArgsConstructor
    static class Location implements Cloneable{
        String street;
        int roomNo;

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
}

